<?php
class StoreProduct extends StoreAppModel {
	var $name = 'StoreProduct';
	
	var $displayField = 'name';
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $virtualFields = array(
	    'full_text_serch' => 'StoreProduct.name || StoreProduct.serial_number || StoreProduct.keywords || StoreProduct.content'
	);
	
	public $actsAs = array('Searchable');		
			
	public $filterArgs = array(
		array('name' => 'full_text_serch', 'type' => 'like')
//		,array('name' => 'model_id', 'type' => 'value')
		,array('name' => 'model_id', 'type' => 'subquery', 'method' => 'findByModels', 'field' => 'StoreProduct.id')
		,array('name' => 'category_id', 'type' => 'subquery', 'method' => 'findByCategory', 'field' => 'StoreProduct.id')
		,array('name' => 'price_detaliczna', 'type' => 'expression', 'method' => 'makePriceCondition', 'field' => 'StoreProduct.price_detaliczna BETWEEN ? AND ?')
	);
	
	

		
	var $hasAndBelongsToMany = array(
		'StoreCategory' =>
			array( 	'className' => 'Store.StoreCategory',
					'joinTable' => 'store_product_categories',
					'foreignKey' => 'product_id',
					'associationForeignKey' => 'category_id',
					'order' => 'StoreCategory.name',
					'unique' => true
				 ),
		'StoreUpload' =>
			array( 	'className' => 'Store.StoreUpload',
					'joinTable' => 'store_product_uploads',
					'foreignKey' => 'product_id',
					'associationForeignKey' => 'upload_id',
					'order' => 'StoreUpload.name',
					'unique' => true
				 ),
		'StoreType' =>
			array( 	'className' => 'Store.StoreType',
					'joinTable' => 'store_product_types',
					'foreignKey' => 'product_id',
					'associationForeignKey' => 'type_id',
					'order' => 'StoreType.name',
					'unique' => true
				 ),
		'StoreModel' =>
			array( 	'className' => 'Store.StoreModel',
					'joinTable' => 'store_product_models',
					'foreignKey' => 'product_id',
					'associationForeignKey' => 'model_id',
					'order' => 'StoreModel.name',
					'unique' => true
				 )
	);
	

	
	var $belongsTo = array(
		'StoreUpload' => array(
			'className' => 'StoreUpload',
			'foreignKey' => 'upload_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'StoreGroup' => array(
			'className' => 'StoreGroup',
			'foreignKey' => 'group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'StoreTax' => array(
			'className' => 'StoreTax',
			'foreignKey' => 'tax_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'StoreUnit' => array(
			'className' => 'StoreUnit',
			'foreignKey' => 'unit_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
//		'StoreModel' => array(
//			'className' => 'StoreModel',
//			'foreignKey' => 'model_id',
//			'conditions' => '',
//			'fields' => '',
//			'order' => ''
//		),
		'StoreCurrency' => array(
			'className' => 'StoreCurrency',
			'foreignKey' => 'currency_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	var $validate = array(

		'name' => array( 
       		'rule' => 'notEmpty',
        	'message' => 'This field cannot be left blank'
    	),
//		'model_id' => array( 
//       		'rule' => 'notEmpty',
//        	'message' => 'This field cannot be left blank'
//    	),
		'price_detaliczna' => array( 
       		'rule' => 'notEmpty',
        	'message' => 'This field cannot be left blank'
    	)
	);
	
	public function findByModels($data = array()) {
		$this->StoreProductModel->Behaviors->attach('Containable', array('autoFields' => false));
		$this->StoreProductModel->Behaviors->attach('Search.Searchable');
		$query = $this->StoreProductModel->getQuery('all', array(
			'conditions' => array('StoreProductModel.model_id'  => $data['model_id']),
			'fields' => array('product_id')
		));
		return $query;
	}
	
	public function findByCategory($data = array()) {
		$this->StoreProductCategory->Behaviors->attach('Containable', array('autoFields' => false));
		$this->StoreProductCategory->Behaviors->attach('Search.Searchable');
		$query = $this->StoreProductCategory->getQuery('all', array(
			'conditions' => array('StoreProductCategory.category_id'  => $data['category_id']),
			'fields' => array('product_id')
		));
		return $query;
	}
	
	function makePriceCondition($data=null){
		if(isset($data['price_detaliczna'])){
			return explode(';', $data['price_detaliczna']);
		}else{
			return false;
		}
	}
}
?>